package com.fitzone.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fitzone.common.exception.LoginFailedException;
import com.fitzone.model.dto.CoachDTO;
import com.fitzone.model.dto.CoachMiniDTO;
import com.fitzone.model.entity.Coach;
import com.fitzone.model.entity.CoachPhoto;
import com.fitzone.model.entity.Store;
import com.fitzone.model.entity.SysUser;
import com.fitzone.model.vo.CoachMiniVO;
import com.fitzone.model.vo.CoachVO;
import com.fitzone.model.vo.StoreMiniVO;
import com.fitzone.web.mapper.CoachMapper;
import com.fitzone.web.mapper.CoachPhotoMapper;
import com.fitzone.web.mapper.StoreMapper;
import com.fitzone.web.service.CoachPhotoService;
import com.fitzone.web.service.CoachService;
import com.fitzone.web.service.StoreService;
import com.fitzone.web.service.SysUserService;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.awt.event.ItemEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CoachServiceImpl extends ServiceImpl<CoachMapper, Coach> implements CoachService {
    @Autowired
    private CoachMapper coachMapper;
    @Autowired
    private CoachPhotoService coachPhotoService;
    @Autowired
    private StoreMapper storeMapper;

    @Override
    public void updateCoach(CoachDTO coachDTO) {
        Coach coach = new Coach();
        BeanUtils.copyProperties(coachDTO,coach);
        coachMapper.updateById(coach);

        LambdaQueryWrapper<CoachPhoto> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CoachPhoto::getCoachId,coach.getId());
        coachPhotoService.remove(queryWrapper);

        List<CoachPhoto> cert = coachDTO.getCert().stream().map(item -> {
            System.out.println("item = " + item);
            CoachPhoto coachPhoto = new CoachPhoto();
//            coachPhoto.setId(coachDTO.getId());;
            coachPhoto.setCoachId(coach.getId());
            coachPhoto.setName(item.get("name"));
            coachPhoto.setUrl(item.get("url"));
            coachPhoto.setType(0);
            return coachPhoto;
        }).collect(Collectors.toList());
        coachPhotoService.saveBatch(cert);

        List<CoachPhoto> personal = coachDTO.getPersonal().stream().map(item -> {
            System.out.println(item);
            CoachPhoto coachPhoto = new CoachPhoto();
//            coachPhoto.setId(coachDTO.getId());
            coachPhoto.setCoachId(coach.getId());
            coachPhoto.setName(item.get("name"));
            coachPhoto.setUrl(item.get("url"));
            coachPhoto.setType(1);
            return coachPhoto;
        }).collect(Collectors.toList());
        coachPhotoService.saveBatch(personal);
    }

    @Override
    @Transactional
    public void saveCoach(CoachDTO coachDTO) {
        Coach coach = new Coach();
        BeanUtils.copyProperties(coachDTO,coach);
        coach.setStatus(1);
        coach.setCount(0);
        coachMapper.insert(coach);
        List<CoachPhoto> cert = coachDTO.getCert().stream().map(item -> {
            CoachPhoto coachPhoto = new CoachPhoto();
            coachPhoto.setCoachId(coach.getId());
            coachPhoto.setName(item.get("name"));
            coachPhoto.setUrl(item.get("url"));
            coachPhoto.setType(0);
            return coachPhoto;
        }).collect(Collectors.toList());
        coachPhotoService.saveBatch(cert);

        List<CoachPhoto> personal = coachDTO.getPersonal().stream().map(item -> {
            CoachPhoto coachPhoto = new CoachPhoto();
            coachPhoto.setCoachId(coach.getId());
            coachPhoto.setName(item.get("name"));
            coachPhoto.setUrl(item.get("url"));
            coachPhoto.setType(1);
            return coachPhoto;
        }).collect(Collectors.toList());
        coachPhotoService.saveBatch(personal);
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        Coach coach = new Coach();
        coach.setStatus(status);
        coach.setId(id);
        this.updateById(coach);
    }

    @Override
    public CoachVO getCoachById(Long id) {
        Coach coach = coachMapper.selectById(id);
        CoachVO coachVO = new CoachVO();
        BeanUtils.copyProperties(coach,coachVO);

        LambdaQueryWrapper<CoachPhoto> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CoachPhoto::getCoachId, coach.getId());
        List<CoachPhoto> coachPhotos = coachPhotoService.list(queryWrapper);
        ArrayList<Map<String, String>> cert = new ArrayList<>();
        ArrayList<Map<String, String>> personal = new ArrayList<>();
        for (CoachPhoto coachPhoto : coachPhotos) {
            HashMap<String, String> map = new HashMap<>();
            map.put("url", coachPhoto.getUrl());
            map.put("name", coachPhoto.getName());
            if (coachPhoto.getType() == 0) {
                cert.add(map);
            } else {
                personal.add(map);
            }
        }
        coachVO.setCert(cert);
        coachVO.setPersonal(personal);
        return coachVO;
    }

    @Override
    public List<CoachMiniVO> miniCoachList(CoachMiniDTO coachMiniDTO) {
        LambdaQueryWrapper<Store> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(coachMiniDTO.getCity() != null,Store::getCity,coachMiniDTO.getCity())
                .eq(coachMiniDTO.getStoreName() != null,Store::getStoreName,coachMiniDTO.getStoreName());

        List<Store> stores = storeMapper.selectList(queryWrapper);
        ArrayList<CoachMiniVO> list = new ArrayList<>();
        for (Store store : stores) {
            CoachMiniVO coachMiniVO = new CoachMiniVO();
            coachMiniVO.setCity(store.getCity());
            coachMiniVO.setAddress(store.getAddress());
            coachMiniVO.setStoreName(store.getStoreName());
            coachMiniVO.setStoreId(store.getId());
            LambdaQueryWrapper<Coach> coachQueryWrapper = new LambdaQueryWrapper<>();
            coachQueryWrapper.eq(Coach::getStoreId,store.getId())
                                .eq(Coach::getStatus,1);
            List<Coach> coaches = coachMapper.selectList(coachQueryWrapper);
            coachMiniVO.setCoachList(coaches);
            list.add(coachMiniVO);
        }
        return list;
    }
}
